// pages/older/older.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:  60 * 15 * 1000,
    timeData: {},
    date:'',
    show: false,
    startTime:'00:00',
    endTime:'00:00',
    showTime:false,
    currentDate: '12:00',
    minHour: 8,
    maxHour: 23,
    selectedButton:'',
    orderList:[],
    address:[
      {
        "aid": 1,
        "uid": 1,
        "address": "天津市河西区大沽南路1310号‌",
        "isDefault": 0
      },
      {
        "aid": 2,
        "uid": 1,
        "address": "天津市南开区卫津路94号",
        "isDefault": 1
      }
    ],
    defaultAddress:'',
    showAddress:false,
    addressList:[],
    notes:'11',
    orderid:'',
    totalPrice:''
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onChange(e) {
    
    this.setData({
      timeData: e.detail,
    });
   
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {

    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    console.log(this.data.date);
  },
  onInput: function (e) {
    const time = e.detail;  // 获取选择的时间
    const selectedButton = this.data.selectedButton;

    if (selectedButton === '11') {
      this.setData({
        startTime: time,
        showTime: false  // 关闭时间选择器
      });
      console.log(this.data.startTime);
    } else if (selectedButton === '22') {
      this.setData({
        endTime: time,
        showTime: false  // 关闭时间选择器
      });
      console.log(this.data.endTime);
    }
  },
  onClick: function (e) {
    const buttonId = e.currentTarget.id;  // 获取按钮的id
    console.log(buttonId);
    if (buttonId === '11') {
      this.setData({
        showTime: true,
        selectedButton: '11'  // 记录当前选择的是哪个按钮
      });
    } else if (buttonId === '22') {
      this.setData({
        showTime: true,
        selectedButton: '22'  // 记录当前选择的是哪个按钮
      });
    }
    console.log(this.data.selectedButton);
  },
  onClose1() {
    this.setData({ showTime: false });
  },

 getAllAddress(){
  wx.request({
    url: 'http://localhost:9001/wx/clearShoppingCart',
    method:'GET',
    withCredentials: true, 
    success:(res)=>{
      this.setData({
        address:res.data.data
      })
       const dea=this.data.address.filter(item => item.isDefault === 1);
       const addressList = this.data.address.map(item => item.address);
       this.setData({
        defaultAddress: dea.address, // 直接将计算结果赋值给 defaultAddress,
        addressList: addressList
      });

      wx.setStorage({
        key:'allAddress',
        data:this.data.address
      })



    }
  })
 },

 onSubmit(){
      // if()
      this.data.orderList.forEach(ShoppingCart=>{
        //  console.log(ShoppingCart.specification);
           const specification=`${ShoppingCart.specification[0]}/${ShoppingCart.specification[1]}/${ShoppingCart.specification[2]}`;
           console.log(specification);
           ShoppingCart.specification = specification;
      })
      const stTime=this.data.date+" "+this.data.startTime+":00";
      const enTime=this.data.date+" "+this.data.endTime+":00";
      console.log(enTime);
if(this.data.timeData.minutes===0){
  wx.showToast({
    title: '支付超时',
    icon: 'none',
    duration: 1500
  });
  
}
else{
  
  wx.request({
    url: 'http://localhost:9001/wx/submitOrder',
    method:'POST',
    withCredentials: true, 
    data:{
      totalPrice:this.data.totalPrice,
      "paymentMethod": "支付宝",
      notes:this.data.notes,
      orderid:this.data.orderid,
      startservicetime:stTime,
      endservicetime:enTime,
      address:this.data.defaultAddress,
      orderdetailList:this.data.orderList,
     
    },
    success:(res)=>{
      wx.showToast({
         title:res.data.data,
        icon: 'none',
        duration: 1500
      });
      wx.switchTab({
        url: '/pages/list/list',
        success:(res3)=>{
          console.log(res3);
        }
      })


    }
  })
}


},
 switchAddress(){
    this.setData({
      showAddress:true
    })
 },
 onClose2(){
  this.setData({
    showAddress:false
  })
 },
confirmAddress(event){
  console.log(event.detail.value);
 this.setData({
  defaultAddress:event.detail.value,
  showAddress:false
 })
},
  onLoad(options) {
    const date = new Date();
    const currentDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
    this.setData({
      'this.data.date':currentDate
    });


      wx.getStorage({
        key:'allCart',
        success:(res)=>{
           this.setData({
             orderList:res.data.orderdetailList,
             totalPrice:res.data.totalPrice
           })
           console.log(this.data.orderList);
        }
      });

      wx.getStorage({
        key:'oriderid',
        success:(res)=>{
          console.log(res.data);
           this.setData({
             orderid:res.data,
           })
        }
      });
     

    this.getAllAddress();

    const dea=this.data.address.filter(item => item.isDefault === 1);
    console.log(dea);
       const addressList = this.data.address.map(item => item.address);
       this.setData({
        defaultAddress: dea[0].address, // 直接将计算结果赋值给 defaultAddress,
        addressList: [addressList]
      });
      console.log(this.data.defaultAddress)
      wx.setStorage({
        key:'allAddress',
        data:this.data.address
      })
  },
  mageAddress(){
    wx.navigateTo({
      url: `/pages/address/address`,
      success: (res) => {
        console.log('跳转成功:', res);
      },
      fail: (err) => {
        console.log('跳转失败:', err);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      console.log('showshow');
      wx.getStorage({
        key:'allAddress',
        success:(res)=>{
          this.setData({
            address:res.data,
            showAddress:false
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})